package com.gimi.cloud.bbp.merchantZone.consumer.service.ueditor.impl;

import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.multipart.support.StandardMultipartHttpServletRequest;

import com.gimi.cloud.bbp.common.utils.reqhead.Util;
import com.gimi.cloud.bbp.merchantZone.consumer.feign.UeditorFeignService;
import com.gimi.cloud.bbp.merchantZone.consumer.service.ueditor.UeditorService;

/**
 * 
 * @author hetianshou
 * @Version 1.0
 * @Description Ueditor编辑器serviceImpl
 * @create 2018-02-05
 */
@Service
public class UeditorServiceImpl implements UeditorService{
	private final UeditorFeignService ueditorFeignService;

	@Autowired
	public UeditorServiceImpl(UeditorFeignService ueditorFeignService) {
		this.ueditorFeignService = ueditorFeignService;
	}
	/**
     * editor操作
     */
    @Override
    public void ueditorAction(HttpServletRequest request, HttpServletResponse response) throws Exception {
        MultiValueMap<?, ?> multipartFiles = new LinkedMultiValueMap<>();
        try {
            if(request instanceof StandardMultipartHttpServletRequest) {
                StandardMultipartHttpServletRequest multipartHttpServletRequest = (StandardMultipartHttpServletRequest) request;
                multipartFiles = multipartHttpServletRequest.getMultiFileMap();
            }
        }catch (Exception e){
            e.printStackTrace();
        }

        //请求参数
        Map<String,String> paramsData = new HashMap<>();
        Enumeration<String> names = request.getParameterNames();
        while (names.hasMoreElements()){
            String name = names.nextElement();
            paramsData.put(name,request.getParameter(name));
        }

        Util.sendResponse(ueditorFeignService.ueditorAction(paramsData,multipartFiles),response);
    }

}
